Design Patterns Used Internally in ASP.NET Core (Real Usage)

 

🔥 Design Patterns Used Internally in ASP.NET Core (Real Usage)

This answer shows framework-level understanding, not just theory.


1️⃣ Singleton Pattern – Used Everywhere in ASP.NET Core

📍 Where it is used

  • ILogger

  • IConfiguration

  • IMemoryCache

  • IHttpContextAccessor

📍 How ASP.NET Core uses it

When you register services like:

builder.Services.AddSingleton<IMyService, MyService>();

ASP.NET Core creates only one instance and shares it across the app.

📍 Real example

public class MyService { public Guid Id { get; } = Guid.NewGuid(); }

Injected everywhere → same Id value.

🗣 Interview line

“ASP.NET Core uses Singleton for shared services like logging, configuration, and caching to ensure a single instance across requests.”


2️⃣ Factory Pattern – Built into Dependency Injection

📍 Where it is used

  • ILoggerFactory

  • DbContextFactory

  • HttpClientFactory

📍 Real example: HttpClientFactory

builder.Services.AddHttpClient();

ASP.NET Core internally uses a Factory to create HttpClient instances safely.

📍 Why this matters

  • Prevents socket exhaustion

  • Centralizes creation logic

  • Allows retry, logging, policies

🗣 Interview line

“ASP.NET Core uses Factory pattern in HttpClientFactory and ILoggerFactory to centralize and control object creation.”


3️⃣ Builder Pattern – Startup & Middleware Pipeline

📍 Where it is used

  • WebApplicationBuilder

  • HostBuilder

  • IApplicationBuilder

📍 Example

var builder = WebApplication.CreateBuilder(args); builder.Services .AddControllers() .AddSwaggerGen() .AddAuthentication();

This is Builder Pattern (fluent API).

📍 Why useful

  • Step-by-step configuration

  • Clean & readable

  • Avoids large constructors

🗣 Interview line

“The ASP.NET Core startup uses the Builder pattern to configure services and middleware step by step.”


4️⃣ Adapter Pattern – Middleware & Legacy Integration

📍 Where it is used

  • Middleware adapting HTTP requests

  • Legacy APIs

  • External libraries

📍 Middleware example

Middleware adapts raw HTTP request to ASP.NET Core pipeline:

app.Use(async (context, next) => { // context adapts low-level HTTP await next(); });

ASP.NET Core adapts:

  • Kestrel server → HttpContext

  • Old systems → modern pipeline

🗣 Interview line

“Middleware acts as an Adapter between raw HTTP requests and application logic.”


5️⃣ Decorator Pattern – Middleware & Filters

📍 Where it is used

  • Middleware

  • Filters (Authorization, Action Filters)

  • Logging, caching, exception handling

📍 Example: Middleware pipeline

app.UseAuthentication(); app.UseAuthorization(); app.UseResponseCompression();

Each middleware:

  • Wraps the next middleware

  • Adds behavior before/after

This is Decorator Pattern.

🗣 Interview line

“ASP.NET Core middleware pipeline is a classic example of the Decorator pattern.”


6️⃣ Observer Pattern – Events & Notifications

📍 Where it is used

  • Logging

  • Diagnostic listeners

  • Application events

  • Domain events

📍 Example

ILogger.LogInformation("User logged in");

Multiple providers (Console, App Insights, File) receive the log.

📍 Why Observer?

  • One event → many subscribers

  • Loose coupling

🗣 Interview line

“ASP.NET Core logging and diagnostic listeners follow the Observer pattern.”


7️⃣ Strategy Pattern – Authentication, Caching, Policies

📍 Where it is used

  • Authentication schemes

  • Authorization policies

  • Output caching

  • Retry policies

📍 Example: Authentication strategy

builder.Services.AddAuthentication() .AddJwtBearer() .AddCookie();

ASP.NET Core selects strategy at runtime.

📍 Why useful

  • Switch behavior dynamically

  • No if/else logic

🗣 Interview line

“Authentication and authorization in ASP.NET Core use Strategy pattern to switch algorithms at runtime.”


🧠 Bonus: Extra Patterns Interviewers Love

PatternASP.NET Core Example
Dependency InjectionBuilt-in IoC container
Chain of ResponsibilityMiddleware pipeline
Template MethodController lifecycle
ProxyAPI Gateway, Ocelot
FacadeControllers hiding service complexity

🎯 One Perfect Interview Answer (Memorize This)

“ASP.NET Core internally uses multiple design patterns. Singleton for logging and configuration, Factory for HttpClient and ILogger creation, Builder for application startup, Decorator and Chain of Responsibility in middleware pipeline, Adapter for HTTP abstraction, Observer for logging and diagnostics, and Strategy for authentication and authorization mec

Comments

Popular posts from this blog

.NET Core Interview Questions and Answers for 10+ Years Experienced Professionals

What are SOLID Principles?

.NET Core Senior Interview Q&A